define(
    function (require) {
        require('tpl!./form.tpl.html');

        var util = require('common/util');

        var BaseFormView = require('common/FormView');

        function MallFormView() {
            BaseFormView.apply(this, arguments);
        }

        MallFormView.prototype.template = 'mallForm';

        MallFormView.prototype.enterDocument = function() {
            BaseFormView.prototype.enterDocument.apply(this, arguments);
            this.get('address').query = util.bind(getAreaCode, this);
        };

        /**
         * 从表单中获取实体数据
         *
         * @return {Object}
         */
        MallFormView.prototype.getEntity = function () {
            var entity = BaseFormView.prototype.getEntity.apply(this, arguments);
            entity = util.omit(entity, 'files');
            if (util.isString(entity.districtid)) {
                entity.districtid = entity.districtid.replace(/^\w+?_/, '');
            }
            return entity;
        };

        function getAreaCode() {
            var cityControl = this.get('district');
            var cityValue = cityControl.getValue();
            var areaCode = '021';
            var city = util.walkTree(
                this.model.get('cityList'),
                function (item) {
                    if (item.value === cityValue) {
                        areaCode = item.areaCode;
                        return false;
                    }
                }
            );
            return {city: areaCode};
        }

        require('er/util').inherits(MallFormView, BaseFormView);
        return MallFormView;
    }
);
